<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
    <script src='../vue.global.js'></script>
</head>

<body>
    <div id="computed-basics">
        {{count}}
        <button @click="count++">add</button>

        <p>Has published books:</p>
        <span>{{ hasBooksMethod() }}</span>
        <span>{{ hasBooksComputed }}</span>
    </div>

    <script>
        Vue.createApp({
            data() {
                return {
                    count: 0,
                    author: {
                        name: 'John Doe',
                        books: [
                            'Vue 2 - Advanced Guide',
                            'Vue 3 - Basic Guide',
                            'Vue 4 - The Mystery'
                        ]
                    }
                }
            },

            methods: {
                hasBooksMethod() {
                    console.log("hasBooksMethod");
                    return this.author.books.length > 0 ? 'Yes' : 'No'
                }
            },

            /* 【计算而来的】属性——计算属性 */
            computed: {
                // 【计算属性】：虽然实际上是一个函数 但模板上使用的时候当做属性使用
                // {{hasBooksComputed}} v-xxx="hasBooksComputed"

                // 当依赖的一手数据this.author.books.length没有发生变化时 直接使用上一次的计算缓存
                // 当依赖的一手数据发生变化时 调用函数重新计算
                // 计算属性的 getter
                hasBooksComputed() {
                    console.log("hasBooksComputed");
                    return this.author.books.length > 0 ? 'Yes' : 'No'
                }
            }

        }).mount('#computed-basics')
    </script>
</body>

</html>